//
//  YXViewAnimation.swift
//  
//
//  Created by tangbl93 on 2021/2/25.
//

import UIKit
import ViewAnimator
import AnimationDeamon

// MARK: YXViewAnimation aka UIViewAnimation
public final class YXViewAnimation {
    
    /// spin around
    public static func spin(view: UIView, completionHandler completion: VoidClosure? = nil) {
        UIView.animate(withDuration: 0.32, delay: 0, options: [.curveEaseIn]) {
            view.layer.transform = CATransform3DMakeRotation(.pi, 0, 0, 1)
        } completion: { (_) in }
        
        yx_delay(0.2) {
            UIView.animate(withDuration: 0.70, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.2, options: [.curveEaseOut]) {
                view.layer.transform = CATransform3DMakeRotation(.pi * 2, 1, 0, -1)
            } completion: { (_) in
                completion?()
            }
        }
    }
    
    /// zoom
    public static func zoom(view: UIView,
                            from min: CGFloat = 0.8, to max: CGFloat = 1.5,
                            duration: TimeInterval = 0.6,
                            repeats: Bool = true) {
        
        let animations: [Animation] = [
            AnimationType.zoom(scale: min),
            AnimationType.zoom(scale: max),
            AnimationType.zoom(scale: 1.0),
        ]
        
        let options: UIView.AnimationOptions = [.repeat, .autoreverse, .curveLinear,
                                                .curveEaseInOut, .allowUserInteraction]
        
        view.animate(animations: animations, reversed: false,
                     initialAlpha: 1, finalAlpha: 1, delay: 0,
                     duration: duration, options: options) {}
        
        // Deamon Animation
        AnimationDeamon.deamon(view: view) { (view) in
            zoom(view: view, from: min, to: max, duration: duration, repeats: repeats)
        }
    }
    
    /// clear all animations
    public static func clear(view: UIView) {
        view.layer.removeAllAnimations()
    }
}
